package com.lanou.controller;

import com.google.gson.Gson;
import com.lanou.pojo.Orders;
import com.lanou.service.SelectService;
import com.lanou.utils.AjaxResult;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet(name = "OrdersServlet",urlPatterns = "/orders")
public class OrdersServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
    private SelectService selectService = new SelectService();
//      显示订单
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession();
        String stringuid = String.valueOf(session.getAttribute("id"));
        response.setContentType("application/json;charset=utf-8");
        AjaxResult ar = new AjaxResult();
        PrintWriter out = response.getWriter();
        if ("null".equals(stringuid)){
            ar.setCode(1003);
            ar.setMsg("请先登录");
        }else {
            int uid = Integer.parseInt(stringuid);
            String username = selectService.selectUsernameByid(uid);
            List<Orders> orders = selectService.selectOrderByuid(uid);
            if (orders!=null){
                ar.setCode(1000);
                ar.setMsg("查询成功");
                ar.setData(orders);
            }else {
                ar.setCode(1001);
                ar.setMsg("查询失败");
            }
        }
        out.write(new Gson().toJson(ar));
        out.close();
    }
}
